package hs.avr_net_io_panel;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private NetworkService.NetworkServiceBinder mNetworkServiceBinder;
	
	private final Handler messageHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			
			final Bundle bundle = msg.getData();
			final int[] adc = bundle.getIntArray("adc");
			final boolean[] digitalInputValues = bundle.getBooleanArray("digitalInputs");
			
			final TextView textViewADC1 = (TextView) findViewById(R.id.TextViewADC1);
			textViewADC1.setText(String.valueOf(adc[0]));
			final TextView textViewADC2 = (TextView) findViewById(R.id.TextViewADC2);
			textViewADC2.setText(String.valueOf(adc[1]));
			final TextView textViewADC3 = (TextView) findViewById(R.id.TextViewADC3);
			textViewADC3.setText(String.valueOf(adc[2]));
			final TextView textViewADC4 = (TextView) findViewById(R.id.TextViewADC4);
			textViewADC4.setText(String.valueOf(adc[3]));
			super.handleMessage(msg);
		}
	};

	private ServiceConnection mNetworkServiceConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder binder) {
			mNetworkServiceBinder = (NetworkService.NetworkServiceBinder) binder;
			mNetworkServiceBinder.setActivityCallbackHandler(messageHandler);
			//mNetworkServiceBinder.connectToAvrNetIO();
			Log.d("DEBUG:", "Service connected from MainActivity");
		}

		public void onServiceDisconnected(ComponentName className) {
			Log.d("DEBUG:", "Service disconnected from MainActivity");			
		}
	};		
	
	@Override
	protected void onResume() {
		super.onResume();
		final Intent networkIntent = new Intent(this, NetworkService.class);
		boolean tmp = bindService(networkIntent, mNetworkServiceConnection, 0);		
		
	}
	
	@Override
	protected void onPause() {
		unbindService(mNetworkServiceConnection);
		
		super.onPause();
	}
	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main); // erzeuge GUI aus XML
		//mNetworkServiceBinder.connectToAvrNetIO();
	}

	/*
	 * @Override public void onResume() { startService(new
	 * Intent(MainActivity.this,NetworkService.class)); }
	 */

	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu); // Menue aus XML generieren
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.optMenu_Settings:
			Intent intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}

	public void startService(final View view) {
		startService(new Intent(this, NetworkService.class));

	}
	
	public void stopService(final View view) {
		//startService(new Intent(this, NetworkService.class));
		stopService(new Intent(this, NetworkService.class));

	}

	public void connect(final View view) {
		/* get information from AVR-NET-IO */
		/*
		final SharedPreferences settings = SettingsActivity
				.getAppSettings(this);
		final String ipAddress = settings.getString("ipaddress", "unknown");
		final String port = settings.getString("port", "0");
		final String timeout = settings.getString("timeout", "10000");
		*/
		/*
		 * this.communication = new AvrNetIo( ipAddress, Integer.valueOf(port)
		 * .intValue(), Integer.valueOf(timeout).intValue() );
		 * 
		 * Object[] arguments = { ipAddress, port }; String result =
		 * MessageFormat.format("IP: {0} Port: {1}", arguments); final
		 * ProgressDialog pD = ProgressDialog.show(this,
		 * "Connecting to AVR-NET-IO", result, false, true);
		 * 
		 * new Thread() { public void run() { testfunktion(); pD.dismiss(); }
		 * }.start();
		 */

		// stopService(new Intent(MainActivity.this,NetworkService.class));
		
		mNetworkServiceBinder.readValuesFromAvrNetIo();

	}

	/*
	public boolean testfunktion() {
		if (communication.connect()) {
			String netmask = communication.getMask();
			String ip = communication.getIP();
			String kk = communication.getGW();
		} else {

		}
		return true;
	}*/
}